package winter_vacation_homework.day_10;

import com.sun.org.apache.xalan.internal.xsltc.runtime.Node;

import java.util.Arrays;
import java.util.LinkedList;

/**
 * 1、给定一个数组，将数组中的元素向右移动 k 个位置，其中 k 是非负数.【LeetCode题号: 189. 旋转数组】https://leetcode-cn.com/problems/rotate-array/
 * 示例 1:
 * 输入: nums = [1,2,3,4,5,6,7], k = 3
 * 输出: [5,6,7,1,2,3,4]
 * 解释:
 * 向右轮转 1 步: [7,1,2,3,4,5,6]
 * 向右轮转 2 步: [6,7,1,2,3,4,5]
 * 向右轮转 3 步: [5,6,7,1,2,3,4]
 * 示例2：
 * 输入：nums = [-1,-100,3,99], k = 2
 * 输出：[3,99,-1,-100]
 * 解释:
 * 向右轮转 1 步: [99,-1,-100,3]
 * 向右轮转 2 步: [3,99,-1,-100]
 */
public class Question1 {
    public static void rotate(int[] nums, int k) {
       
        LinkedList linkedList = new LinkedList();
        for (int i = 0; i < nums.length; i++) {
            linkedList.addLast(nums[i]);
        }

        for(int i = 0; i < k; i++){
            linkedList.addFirst(linkedList.get(nums.length - 1));

            linkedList.remove(nums.length);
        }
        for (int i = 0; i < nums.length; i++) {
            nums[i] = (int)linkedList.get(i);
        }
        System.out.println(Arrays.toString(nums));
    }

    public static void main(String[] args) {
        int[] nums = new int[]{1,2,3,4,5,6,7};
        rotate(nums,3);
    }
}